package j2024.j202412;

import java.util.Arrays;

public class j1213 {
    public void setZeroes(int[][] matrix) {
        int m = matrix.length, n = matrix[0].length;
        boolean col = false,row = false;
        for (int i = 0; i < m; i++) {
            if(matrix[i][0] == 0){
                row = true;
                break;
            }
        }
        for (int j = 0; j < n; j++) {
            if(matrix[0][j] == 0){
                col = true;
                break;
            }
        }
        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                if(matrix[i][j] == 0){
                    matrix[i][0] = 0;
                    matrix[0][j] = 0;
                }
            }
        }
        for (int j = 1; j < n; j++) {
            if(matrix[0][j] == 0){
                for (int i = 1; i < m; i++) {
                    matrix[i][j] = 0;
                }
            }
        }
        for (int i = 1; i < m; i++) {
            if(matrix[i][0] == 0){
                Arrays.fill(matrix[i],0);
            }
        }
        if(row){
            for (int i = 0; i < m; i++) {
                matrix[i][0] = 0;
            }
        }
        if(col){
            for (int j = 0; j < n; j++) {
                matrix[0][j] = 0;
            }
        }
    }
}
